package com.osgi.desktop.ui.plugins.dialog;

import com.onetwork.core.ui.components.dialog.actions.CancelaAction;
import com.onetwork.core.ui.components.dialog.actions.ConfirmaAction;
import com.onetwork.core.ui.components.dialog.listeners.DialogListener;
import com.osgi.desktop.domain.SiteUpdate;
import java.awt.Color;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class SiteUpdateDialog extends javax.swing.JDialog {
   
    private Border defaultBorder;
    private Border errorBorder;
    private SiteUpdate siteUpdate;           
    private final JFileChooser fileChooser;
    public SiteUpdateDialog(DialogListener dialogListener) {
        this.setTitle("Site Update");
        this.setModal(true);
        this.fileChooser = new JFileChooser();
        this.fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        initComponents();
        this.defaultBorder = nomeTextField.getBorder();
        this.errorBorder = BorderFactory.createLineBorder(Color.RED);
        this.mainToolBar.add(new ConfirmaAction(dialogListener));
        this.mainToolBar.add(new CancelaAction(dialogListener));
    }

    public void clear() {
        siteUpdate = null;
        nomeTextField.setText("");
        urlTextField.setText("");
        nomeTextField.setBorder(defaultBorder);
        urlTextField.setBorder(defaultBorder);
    }

    public SiteUpdate getSiteUpdate() {       
        if (validaForm()) {    
            String nome = nomeTextField.getText();
            String url = urlTextField.getText();
            if (siteUpdate == null) siteUpdate = new SiteUpdate();
            siteUpdate.setNome(nome);
            siteUpdate.setUrl(url);             
        }
        return siteUpdate;
    }

    @Override
    public void setVisible(boolean visible) {
        this.clear();
        super.setVisible(visible);
    }
    
    public void setVisible(boolean b, SiteUpdate siteUpdate) {        
        if (siteUpdate == null) throw new IllegalArgumentException("SiteUpdate Inválido.");
        this.siteUpdate = siteUpdate;
        this.nomeTextField.setText(this.siteUpdate.getNome());
        this.urlTextField.setText(this.siteUpdate.getUrl());
        super.setVisible(b);
    }
    
    public boolean validaForm(){
        boolean nomeValido = isValidField(nomeTextField);
        boolean urlValida = isValidField(urlTextField);
        return (nomeValido && urlValida);
    }

    private boolean isValidField(JTextField textField){
        String value = textField.getText();
        boolean isValid = (value != null && value.trim().length() > 0);
        if (!isValid) textField.setBorder(errorBorder);
        else textField.setBorder(defaultBorder);
         return isValid;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainToolBar = new javax.swing.JToolBar();
        nomeLabel = new javax.swing.JLabel();
        nomeTextField = new javax.swing.JTextField();
        urlLabel = new javax.swing.JLabel();
        urlTextField = new javax.swing.JTextField();
        arqLocalButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        mainToolBar.setFloatable(false);
        mainToolBar.setRollover(true);

        nomeLabel.setText("Nome");

        urlLabel.setText("Url");

        arqLocalButton.setText("Arq. Local");
        arqLocalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                arqLocalButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(urlLabel)
                    .addComponent(nomeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(nomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(urlTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(arqLocalButton)
                .addContainerGap())
            .addComponent(mainToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nomeLabel)
                    .addComponent(nomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(urlLabel)
                    .addComponent(urlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(arqLocalButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-514)/2, (screenSize.height-150)/2, 514, 150);
    }// </editor-fold>//GEN-END:initComponents

private void arqLocalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_arqLocalButtonActionPerformed
    int returnVal = fileChooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {        
        try {
            this.urlTextField.setText(fileChooser.getSelectedFile().toURI().toURL().toString());
        } catch (MalformedURLException ex) {
            Logger.getLogger(SiteUpdateDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}//GEN-LAST:event_arqLocalButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton arqLocalButton;
    private javax.swing.JToolBar mainToolBar;
    private javax.swing.JLabel nomeLabel;
    private javax.swing.JTextField nomeTextField;
    private javax.swing.JLabel urlLabel;
    private javax.swing.JTextField urlTextField;
    // End of variables declaration//GEN-END:variables

}
